package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.entity.Seller;
import com.github.pagehelper.Page;
import com.service.SellerService;
import com.util.VeDate;

@RestController // 定义为控制器 返回JSON类型数据
@RequestMapping(value = "/seller", produces = "application/json; charset=utf-8") // 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class SellerController { // extends BaseController {
	// TODO Auto-generated method stub

	@Autowired // @Autowired的作用是自动注入依赖的ServiceBean
	private SellerService sellerService;

	// 预处理 获取基础参数
	@GetMapping(value = "createSeller.action")
	public Map<String, Object> createSeller() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("today", VeDate.getStringDateShort());
		return map;
	}

	// 查询全部商户数据 在下拉菜单中显示
	@GetMapping(value = "getAllSeller.action")
	public List<Seller> getAllSeller() {
		return this.sellerService.getAllSeller();
	}

	// 按关键字查询商户数据 在下拉菜单中显示
	@GetMapping(value = "getSellerMap.action")
	public Map<String, Object> getSellerMap(String keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		Seller seller = new Seller();
		seller.setUsername(keywords);
		List<Seller> list = this.sellerService.getSellerByLike(seller);
		map.put("data", list);
		return map;
	}

	// 通过AJAX在表格中显示商户数据
	@GetMapping(value = "getSellerByPage.action")
	public Map<String, Object> getSellerByPage(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Seller> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		List<Seller> list = this.sellerService.getAllSeller();
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示商户数据
	@GetMapping(value = "getSeller.action")
	public Map<String, Object> getSeller(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit, String keywords) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Seller> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Seller seller = new Seller();
		seller.setUsername(keywords);
		List<Seller> list = this.sellerService.getSellerByLike(seller);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示商户数据
	@GetMapping(value = "getOwnerSeller.action")
	public Map<String, Object> getOwnerSeller(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit, String id) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Seller> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Seller seller = new Seller();
		// seller.setAdminid(id);
		List<Seller> list = this.sellerService.getSellerByLike(seller);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 按主键查询商户数据
	@GetMapping(value = "getSellerById.action")
	public Seller getSellerById(String id) {
		Seller seller = this.sellerService.getSellerById(id);
		return seller;
	}

	// TODO Auto-generated method stub
}
//
